/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import e.odbo.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_Location;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.serialize.serializer.SerializerRead;
import com.openbravo.data.loader.serialize.serializer.read.SerializerReadString;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.format.Formats;

import com.openbravo.bean.Location;
import com.openbravo.data.model.Field;

/**
 *
 * @author Administrator
 */
public class LocationDAO extends ModifiedLogDAO<Location>{

    public LocationDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(
                "LOCATIONS",
                 new Field[]{
                     new Field(I_Location.ID,Datas.STRING, Formats.STRING),
                     new Field(I_Location.NAME,Datas.STRING, Formats.STRING),
                     new Field(I_Location.ADDRESS,Datas.STRING, Formats.STRING),
                     new Field(I_Location.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP),
                     new Field(I_Location.ISCUSTOMER,Datas.BOOLEAN, Formats.BOOLEAN),
                     new Field(I_Location.ISVIRTUAL,Datas.BOOLEAN, Formats.BOOLEAN),
                     new Field(I_Location.ISSCORE,Datas.BOOLEAN, Formats.BOOLEAN),
                     new Field(I_Location.ISPREFSELL,Datas.BOOLEAN, Formats.BOOLEAN)
                  },
                new int[]{0});
    }

    public Location readValues(DataRead dr,Location obj) throws BasicException {
        if(obj==null)
        	obj=new Location();
        obj.setID(dr.getString(1));
        obj.setName(dr.getString(2));
        obj.setAddress(dr.getString(3));
        obj.setLastModified(dr.getTimestamp(4));
        obj.setCustomer(dr.getBoolean(5));
        obj.setVirtual(dr.getBoolean(6));
        obj.setScore(dr.getBoolean(7));
        obj.setPrefSell(dr.getBoolean(8));
        return obj;
    }

  public final String findLocationName(String iLocation) throws BasicException {
        return (String) new StaticSentence(s
                , "SELECT NAME FROM LOCATIONS WHERE ID = ?"
                , SerializerWriteString.INSTANCE
                , SerializerReadString.INSTANCE).find(iLocation);
    }

    @Override
    public Class getSuportClass() {
        return Location.class;
    }

    @Override
    public void writeInsertValues(DataWrite dp, Location obj) throws BasicException {
        dp.setString(1, obj.getID());
        dp.setString(2,obj.getName());
        dp.setString(3, obj.getAddress());
        dp.setTimestamp(4,obj.getLastModified());
        dp.setBoolean(5,obj.isCustomer());
        dp.setBoolean(6,obj.isVirtual());
        dp.setBoolean(7,obj.isScore());
        dp.setBoolean(8,obj.isPrefSell());
    }
    
    public boolean isCustomer(String location)throws BasicException {
        return (Boolean) new StaticSentence(s
                , "SELECT ISCUSTOMER FROM LOCATIONS WHERE ID = ?"
                , SerializerWriteString.INSTANCE
                , new SerializerRead(){
			@Override
			public Object readValues(DataRead dr) throws BasicException {
				return dr.getBoolean(1);
			}
	
}).find(location);
    }
}
